﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Team6_Algorithms;
using System.Drawing.Imaging;

//written by Alexander Streng & Marleen Schilt
namespace VisionApplication
{
    class Compare
    {
        private Color[,] originalImageArray;
        private Color[,] noiseImageArray;
        private Color[,] filteredImageArray;
        Bitmap filteredImage;
        Bitmap noiseImage;
        Bitmap originalImage;


        public Compare(Bitmap originalImage, Bitmap noiseImage, Bitmap filteredImage)
        {
            this.originalImage = originalImage;
            this.noiseImage = noiseImage;
            this.filteredImage = filteredImage;
        }

        public bool start()
        {
            originalImageArray = readDataFromImage(originalImage);
            noiseImageArray = readDataFromImage(noiseImage);
            filteredImageArray = readDataFromImage(filteredImage);

            int foutCounter = 0;

            if (originalImageArray.Length == filteredImageArray.Length && originalImageArray.Length == noiseImageArray.Length && filteredImageArray.Length == noiseImageArray.Length)
            {
                for (int y = 0; y < originalImage.Height; y++)
                {
                    for (int x = 0; x < originalImage.Width; x++)
                    {
                        if (((originalImageArray[x, y].R) + (originalImageArray[x, y].G) + (originalImageArray[x, y].B)) != ((filteredImageArray[x, y].R) + (filteredImageArray[x, y].G) + (filteredImageArray[x, y].B)))
                        {
                            int filteredColor = filteredImageArray[x, y].R + filteredImageArray[x, y].G + filteredImageArray[x, y].B;
                            int originalColor = originalImageArray[x, y].R + originalImageArray[x, y].G + originalImageArray[x, y].B;

                            if (filteredColor < originalColor - 30 || filteredColor > originalColor + 30)
                            {
                                foutCounter++;
                            }
                        }
                    }
                }

               double percentage = ((double)foutCounter / (double)originalImageArray.Length) * 100;

               Console.WriteLine("Er is: " + percentage + " % niet gelijk");

            } else {

                Console.WriteLine("De images zijn niet gelijk!");

            }

            return true;
        }

        private Color[,] readDataFromImage(Bitmap b)
        {
            Color[,] temp = new Color[b.Width, b.Height];

            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
               ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

            int stride = bmData.Stride;
            System.IntPtr Scan0 = bmData.Scan0;


            unsafe
            {
                byte* p = (byte*)(void*)Scan0;

                int nOffset = stride - b.Width * 3;
                int nWidth = b.Width - 2;
                int nHeight = b.Height - 2;

                for (int y = 0; y < nHeight; ++y)
                {
                    for (int x = 0; x < nWidth; ++x)
                    {
                        temp[ x , y ] = Color.FromArgb((byte)p[5 + stride], (byte)p[4 + stride], (byte)p[3 + stride]);
                        p += 3;
                    }
                    p += nOffset;
                }
            }

            b.UnlockBits(bmData);

            return temp;
        }
    }
}
